package planeWar.src.com.bjsxt.plane;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyGameFrame extends Frame {

    Image planeImage = GameUtil.getImage("images/plane.png");
    Image bg = GameUtil.getImage("images/bg.jpg");
    Plane p1 = new Plane(planeImage,100,100,6,22,33);

    Shell[] shells= new Shell[50];
  @Override
    public void paint(Graphics g) {

      g.drawImage(bg,0,0,500,500,null);
      p1.drawMyself(g);

      for (Shell s:shells) {
          s.drawMyself(g);
          boolean peng = s.getRec().intersects(p1.getRec());
          if (peng){
              System.out.println("peng");
              p1.live=false;
          }
      }


    }

    public void lauchFrame(){
        this.setTitle("feijidazhan");
        setVisible(true);
        setSize(Context.GAME_WIDTH,Context.GAME_HEIGHT);
        setLocation(1000,100);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();
        this.addKeyListener(new KeyMonitor());

        for (int i=0;i<shells.length;i++){
            shells[i] = new Shell();
        }

    }

    class PaintThread extends Thread{
      public void run(){
          while (true){
              repaint();
              try {
                  Thread.sleep(50);
              } catch (InterruptedException e) {
                  e.printStackTrace();
             }
          }

      }
    }
    class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            p1.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            p1.minusDirection(e);
        }
    }

    private  Image offScreenImage =null;
    public void update(Graphics g){
        if (offScreenImage == null){
            offScreenImage = this.createImage(Context.GAME_WIDTH,Context.GAME_HEIGHT);
        }
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) {
        MyGameFrame gameFrame = new MyGameFrame();
        gameFrame.lauchFrame();
    }

}
